<?php
namespace app\admin\controller;
use think\Db;
class Message  extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['t.n_to'] = $role;
            }
            $map['t.n_type'] = ['in','1,4'];
            if(isset($type)&&$type != ""){
                $map['t.n_type'] = $type;
            }
            if(isset($key)&&$key!="")
            {
                $map['t.n_title|t.n_content'] = ['like',"%" . $key . "%"];
            }
            $u_area = '';
            $s_area = '';
            if(session ('area')){
                $u_area = "u.user_area IN ('".session('area')."') AND";
                $s_area = "s.shop_qu_id IN ('".session('area')."') AND";
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table("((SELECT n_id,n_to,n_title,n_content,n_type,n_time,n_user_id,user_username AS n_user_name FROM up_notice n LEFT JOIN up_u_user u ON u.user_id = n.n_user_id WHERE {$u_area} n_to = 1) UNION ALL (SELECT n_id,n_to,n_title,n_content,n_type,n_time,n_user_id,shop_name AS n_user_name FROM up_notice n LEFT JOIN up_shop s ON s.shop_id = n.n_user_id WHERE {$s_area} n_to = 2)) AS t")
                ->where($map)
                ->count();
            $lists = Db::table("((SELECT n_id,n_to,n_title,n_content,n_type,n_time,n_user_id,user_username AS n_user_name FROM up_notice n LEFT JOIN up_u_user u ON u.user_id = n.n_user_id WHERE {$u_area} n_to = 1) UNION ALL (SELECT n_id,n_to,n_title,n_content,n_type,n_time,n_user_id,shop_name AS n_user_name FROM up_notice n LEFT JOIN up_shop s ON s.shop_id = n.n_user_id WHERE {$s_area} n_to = 2)) AS t")
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('t.n_time DESC')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('message/index');
    }


    public function messageAdd(){
        if(request()->isPost ()){
            extract (input());
            $param = [
                'n_to'=>$to,
                'n_title'=>$title,
                'n_type'=>$type,
                'n_time'=>date('Y-m-d H:i:s')
            ];
//            if($obj == 1){
                $param['n_read'] = ',0,';
                $param['n_delete'] = ',0,';
//            }else{
//                $param['n_read'] = 1;
//                $param['n_delete'] = 1;
                if($to == 1){
                    $param['n_user_id'] = $user;
                }else{
                    $param['n_user_id'] = $shop;
                }

//            }
            if($type == 4){
                $param['n_content'] = $html;
            }else{
                $param['n_content'] = $content;
            }
            $res = Db::name('notice')->insert ($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $user = Db::name('u_user')->where(['user_status'=>1])->page(1,1000)->select();
        $shop = Db::name('shop')->where(['shop_status'=>1,'shop_attestation'=>2])->select();
        $this->assign('user',$user);
        $this->assign('shop',$shop);
        return $this->fetch('message/message_add');
    }


    public function messageDel(){
        extract (input());
        $res = Db::name('notice')->where('n_id',$id)->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

}